package com.eam.service;

import com.yy.base.common.dao.Dao;
import com.yy.base.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class EquipmentInspectionService {
	@Autowired
	private Dao dao;
	
	//加载点检计划编码(组织机构是车间的)
	public List<Map<String,Object>> getEquipmentCheckPlanLists(String orgId) {
		return dao.query("select code, descriptions as BEIZHU from Eam_Check_Plan where state in('CREATE','SPOTCHECK') and ad_org_id = ?" , orgId);
	}
	
	//加载点检计划编码（组织机构不是车间的）
	public List<Map<String,Object>> getEquipmentCheckPlanList() {
		return dao.query("select code, descriptions as BEIZHU from Eam_Check_Plan where state in('CREATE','SPOTCHECK') " );
	}
	
	//加载设备磨具编码
	public List<Map<String,Object>> getEquipmentList(String code) {
		
		return dao.query("select sb.code as SHEBEICODE from Eam_Check_Plan  jh " +
				"left join Eam_Check_Plan_Facilities xq on jh.Eam_Check_Plan_id = xq.Eam_Check_Plan_id " +
				"left join Eam_Facilities sb on xq.Eam_Facilities_id = sb.Eam_Facilities_id " +
				"where jh.code = ? " +
				"and xq.status in('CREATE','SPOTCHECK') ", code);
	}
	
	//加载设备信息及点检项目
	public List<Map<String, Object>> getinformationList(String plancode , String code ){
		
		
		return dao.query("  select sb.code as sbcode , yh.name yhname, fc.name as fcname, sb.name as sbname ,jh.cycle as cycles  ,sb.status as SBStatus  ,djxm.name djname , djxm.code as djcode ,djxm.check_standard as standards,djxm.check_mode as modes " +
				" from Eam_Check_Plan jh   " +
				"left join Eam_Check_Plan_Facilities  xq on jh.Eam_Check_Plan_id = xq.Eam_Check_Plan_id  " +
				"left join Eam_Facilities sb on sb.Eam_Facilities_id = xq.Eam_Facilities_id  " +
				"left join ad_user yh on xq.ad_user_id = yh.ad_user_id " +
				" left join ad_org fc on sb.ad_org_id = fc.ad_org_id  " +
				" left join Eam_Check_Plan_Item xm on xm.Eam_Check_Plan_Facilities_id = xq.Eam_Check_Plan_Facilities_id  " +
				" left join Eam_Check_Item  djxm on xm.Eam_Check_Item_id = djxm.Eam_Check_Item_id  " +
				"where jh.code = ? " +
				"and sb.code = ? " +
				"and xm.status = 'CREATE' ", plancode , code);
	}
	
	//查询CREATEBY和UPDATEBY的ID值
	public List<Map<String,Object>> getEquipmentputCreateby(String userIdtime){
		return dao.query("select CREATEDBY as CREATEDBY, UPDATEDBY as UPDATEDBY from ad_user where ad_user_id = ?",userIdtime);
	}
	
	
	//查询点检计划下设备层的设备责任人ID
	public Map<String, Object> getEquipmentuserId(String username){
		return dao.queryUnique("select ad_user_id as userid from ad_user where name= ?", username);
	}
	
	
	//查询点检项目的ID
	public List<Map<String,Object>> getEquipmentput(String djcode,String plancode , String code){
		
//		String sql = "select xq.Eam_Check_Plan_Item_id as XMID from Eam_Check_Plan_Facilities  zj " +
//				"left join Eam_Facilities sb on zj.Eam_Facilities_id = sb.Eam_Facilities_id " +
//				"left join Eam_Check_Plan jh on jh.Eam_Check_Plan_id =  zj.Eam_Check_Plan_id " +
//				"left join Eam_Check_Plan_Item xq on zj.Eam_Check_Plan_Facilities_id = xq.Eam_Check_Plan_Facilities_id " +
//				"left join Eam_Check_Item xm on xm.Eam_Check_Item_id = xq.Eam_Check_Item_id " +
//				"where xq.status ='CREATE' " +
//				"and xm.code = ? " +
//				" and jh.code = ? " +
//				" and sb.code = ? ";
		
		//System.out.println("sql语句是："+sql);
	
				return dao.query("select xq.Eam_Check_Plan_Item_id as XMID from Eam_Check_Plan_Facilities  zj " +
				"left join Eam_Facilities sb on zj.Eam_Facilities_id = sb.Eam_Facilities_id " +
				"left join Eam_Check_Plan jh on jh.Eam_Check_Plan_id =  zj.Eam_Check_Plan_id " +
				"left join Eam_Check_Plan_Item xq on zj.Eam_Check_Plan_Facilities_id = xq.Eam_Check_Plan_Facilities_id " +
				"left join Eam_Check_Item xm on xm.Eam_Check_Item_id = xq.Eam_Check_Item_id " +
				"where xq.status ='CREATE' " +
				"and xm.code = ? " +
				" and jh.code = ? " +
				" and sb.code = ? ", djcode , plancode , code);
	}
	
	//查询所有要存入记录表的信息
	public List<Map<String, Object>> getAllEquipemt(String id ){
		return dao.query(" select sb.Eam_Facilities_id as FACILITIESID, jh.Eam_Check_Plan_id as EmesFACILITIESRUNTIMELOGID, djxm.Eam_Check_Item_id as EmesRUNTIME " +
				"from Eam_Check_Plan_Item  xm " +
				"left join Eam_Check_Plan_Facilities zj on xm.Eam_Check_Plan_Facilities_id = zj.Eam_Check_Plan_Facilities_id " +
				"left join Eam_Facilities sb on sb.Eam_Facilities_id = zj.Eam_Facilities_id " +
				"left join Eam_Check_Plan jh on jh.Eam_Check_Plan_id = zj.Eam_Check_Plan_id " +
				"left join Eam_Check_Item djxm on xm.Eam_Check_Item_id = djxm.Eam_Check_Item_id " +
				"where xm.Eam_Check_Plan_Item_id =? ", id);
	}
	

	//保存设备点检计划信息
	@SuppressWarnings("unchecked")
	public boolean SavaEquipmentputInsert(String Radio,String facilitiesid,String RemarksNames,String aduserId,String recordCode,String emesmaintainplanid,String emesruntime,String adorgId,String adclientId,String createBy,String updatedBy,String ISACTIVE,Date time) {
		boolean result = false;
		String uuid = StringUtil.getUUID();
		
		String sql =  "INSERT INTO  Eam_Check_Result(Eam_Check_Result_ID,Eam_Facilities_ID,Eam_Check_Plan_ID,Eam_Check_Item_ID,AD_USER_ID,CODE,DESCRIPTION,AD_CLIENT_ID，AD_ORG_ID,ISACTIVE,CREATED,CREATEDBY,UPDATED,UPDATEDBY,CHECKRESULT) " +
				"VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		
		result = dao.executeSql(sql, uuid,facilitiesid,emesmaintainplanid,emesruntime,aduserId,recordCode,RemarksNames,adclientId,adorgId,ISACTIVE,time,createBy,time,updatedBy,Radio) > 0;
		
		return result;
	}
	
	//修改点检项目的状态 ，以及添加处理结果和备注
	@SuppressWarnings("unchecked")
	public boolean SaveEquipmentPlanDetails(String status,String id,String RemarksNames,String Description) {
		boolean result = false;
		
		
		String sql =  "UPDATE Eam_Check_Plan_Item SET status = ?, DESCRIPTION = ? , PROCESSINGRESULTS = ?  WHERE Eam_Check_Plan_Item_id = ?";
		
		result = dao.executeSql(sql, status,Description,RemarksNames,id) > 0;
		
		
		return result;
	}
	
	//修改点检项目的点检结果
		@SuppressWarnings("unchecked")
		public boolean SaveEquipmentPlanDetailsCheckResult(String Radio,String id) {
			boolean result = false;
			
			
			String sql =  "UPDATE Eam_Check_Plan_Item SET CHECKRESULT = ? WHERE Eam_Check_Plan_Item_id = ?";
			
			result = dao.executeSql(sql, Radio,id) > 0;
			
			
			return result;
		}
		
		//修改点检项目的责任人
		@SuppressWarnings("unchecked")
		public boolean SaveEquipmentuserDetailsCheckResult(String userIdtime,String id) {
			boolean result = false;
					
					
			String sql =  "UPDATE Eam_Check_Plan_Item SET ad_user_id = ? WHERE Eam_Check_Plan_Item_id = ?";
					
			result = dao.executeSql(sql, userIdtime,id) > 0;
					
					
			return result;
		}
	
	
	//加载设备信息及点检项目
	public Map<String, Object> getcountinformationList(String plancode , String code ){
		return dao.queryUnique("  select count(*) as numbers" +
				" from Eam_Check_Plan jh   " +
				"left join Eam_Check_Plan_Facilities  xq on jh.Eam_Check_Plan_id = xq.Eam_Check_Plan_id  " +
				"left join Eam_Facilities sb on sb.Eam_Facilities_id = xq.Eam_Facilities_id  " +
				"left join ad_user yh on xq.ad_user_id = yh.ad_user_id " +
				" left join ad_org fc on sb.ad_org_id = fc.ad_org_id  " +
				" left join Eam_Check_Plan_Item xm on xm.Eam_Check_Plan_Facilities_id = xq.Eam_Check_Plan_Facilities_id  " +
				" left join Eam_Check_Item  djxm on xm.Eam_Check_Item_id = djxm.Eam_Check_Item_id  " +
				"where jh.code = ? " +
				"and sb.code = ? " +
				"and xm.status = 'CREATE' ", plancode , code);
	}
	
	//调用存储过程进行状态改变
	@SuppressWarnings("unchecked")
	public boolean getEquipmentputList(String id) {
		boolean result = false;
		
		result = dao.executeSql("{call Emes_P_UPDATE_CHECK(?)}",id) > 0;
		
		return result;
	}
	
	
	
}
